package verbindung;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayList;

/**
 * 
 * @author Nilientes
 *
 */
public class ReceiveBuffer extends Thread {
	private ArrayList<DBDTransStruct> buffer = new ArrayList<DBDTransStruct>();

	private DataInputStream in ;
	
	
	protected ReceiveBuffer(DataInputStream in) {
		super();
		this.in = in;
		
		this.start();
	}


	@Override
	public void run()
	{
		while(true)
		{
			DBDTransStruct tr = new DBDTransStruct();
			
			try {
				tr.receiveStruct(in);
			} catch (IOException e) {
				
			}
			
			buffer.add(tr);
		}
	}
	
	/**
	 * 
	 * @param name
	 * @return returns the last TransStruct to a givven variable name 
	 */
	public DBDTransStruct get(String name)
	{
		DBDTransStruct trst=null;
		
		for(DBDTransStruct t : buffer)
		{
			if(t.name.equals(name))
			{
				trst = t;
				break;
			}
		}
		
		if(trst!=null)
		{
			buffer.remove(trst);
			return trst;
		}
		
		return null;
	}
	
	/**
	 * 
	 * @return returns the recorded buffer
	 */
	public ArrayList<DBDTransStruct> getBuffer()
	{
		return buffer;
	}
	
	
	
	
}
